﻿package com.lsmo.android.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符类型转换类
 * @author syx
 * @version 1.0
 * @项目名称：carfans     
 * @类名称：FormatUtil 
 * @修改时间：2012-3-26 
 * @修改备注：  
 */
public class FormatUtil {
	public static boolean validateMoblie(String phone) {
		int l = phone.length();
		boolean rs = false;
		switch (l) {
//		case 7:
//			if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4}$", phone)) {
//				rs = true;
//			}
//			break;
		case 11:
			if (matchingText("^(13[0-9]|15[0-9]|18[7|8|9|6|5])\\d{4,8}$", phone)) {
				rs = true;
			}
			break;
		default:
			rs = false;
			break;
		}
		return rs;
	}

	private static boolean matchingText(String expression, String text) {
		Pattern p = Pattern.compile(expression); // 正则表达式
		Matcher m = p.matcher(text); // 操作的字符串
		boolean b = m.matches();
		return b;
	}
	
	public static boolean nameFormat(String name){		
		boolean tag = true;
		String exp = "^(?!_)(?!.*?_$)[\u4E00-\u9FA5\\w\\d]{6,32}$";//六位用户名
		Pattern pattern = Pattern.compile(exp);
		Matcher mat = pattern.matcher(name);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
	}
	
	
	public static boolean pwdFormat(String pwd){		
		boolean tag = true;
		//Pattern pattern = Pattern.compile("((?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[_]).{6,32})");  //必须一个大写，一个小写，一个数字，一个_
		Pattern pattern = Pattern.compile("^([\\w\\d]){6,32}$");
		Matcher mat = pattern.matcher(pwd);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
	}
	
	 /**
     * 验证输入的邮箱格式是否符合
     * @param email
     * @return 是否合法
     */
	public static boolean emailFormat(String email)
    {
        boolean tag = true;
        final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(email);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }
	
	/**
	 * 验证输入的字符是否数字 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str){ 
		boolean tag = true;
		String exp = "^[0-9]*[1-9][0-9]*$";
		Pattern pattern = Pattern.compile(exp);
		Matcher mat = pattern.matcher(str);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
	}
	
	/**
	 * 将InputStream类型转换成String类型
	 * @param paramInputStream
	 * @return
	 */
	public static String convertStreamToString(InputStream paramInputStream){
		//方法1
		StringBuffer outStr = new StringBuffer(); 
	    try {
			byte[] b = new byte[4096];
			for (int n; (n = paramInputStream.read(b)) != -1;) { 
				outStr.append(new String(b, 0, n)); 
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
	    return outStr.toString();
		//方法2
//	    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
//	    int i=-1; 
//	    try {
//			while((i=paramInputStream.read())!=-1){ 
//				baos.write(i); 
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} 
//	    return baos.toString(); 
	}

	public static void main(String[] args) {
		System.out.println(validateMoblie("13556898956"));
		System.out.println(emailFormat("syx@gmail.com"));
		System.out.println(pwdFormat("_syx123_"));
		System.out.println(nameFormat("中国yx99"));
		System.out.println(isNumeric("0"));
	}
}
